/*
 * Copyright 2006-2007 S W McIlroy
 * Licensed  under the  Apache License,  Version 2.0  (the "License");
 * you may not use  this file  except in  compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed  under the  License is distributed on an "AS IS" BASIS,
 * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
 * implied.
 *
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.mockfu;

import java.lang.reflect.Method;

/**
 * Thrown when an expectation fails.
 */
public class ExpectationFailure extends AssertionError
{
    private static final long serialVersionUID = -5508802337468803276L;

    public ExpectationFailure(String message)
    {
        super(message);
    }

    ExpectationFailure(Expectable expectation)
    {
        super("Expected " + expectation + ", but it was called " + expectation.getMatchCount() + " times" );
    }

    ExpectationFailure(InvocationContext context)
    {
        super(getMessage(context.getMethod(), context.getArguments()));
    }

    private static String getMessage(Method method, Object... args)
    {
        StringBuilder argsString = new StringBuilder();
        for (Object arg : args)
        {
            if (argsString.length() != 0)
                argsString.append(", ");
            argsString.append(Constraints.toString(arg));
        }
        return "Method call " + method.getDeclaringClass().getSimpleName()
                + "." + method.getName() + "(" + argsString + ") was unexpected.";
    }
}
